<template>
  <div class="container">
    <div class="cardCode-page">
      <div class="wrapper">
        <div class="title">{{ userInfo.name }}<span class="text">专属微名片小程序码</span></div>
        <div class="icon">
          <img class="image" :src="code">
        </div>
        <div class="line"></div>
        <div class="text1">此名片码可印刷在</div>
        <div class="text2">纸质名片、宣传单、海报、易拉宝等宣传册上</div>
        <div class="text3">微信扫一扫名片即可进入微信名片</div>
        <div class="text3">并成为分销下线</div>
      </div>
      <div class="card-show">
        <div class="title">纸质名片展示</div>
        <div class="content">
          <img mode="widthFix" class="image" src="/static/images/card-show.jpg" alt="">
        </div>
      </div>
      <div class="footer">
        <div class="button" @click="save">保存小程序码到手机</div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      code: '',
      userInfo : {}
    }
  },

  onShareAppMessage (res) {
    let self = this;
    let url = `${self.$confs.shareUrl}?id=${self.userInfo.id}`;
    return {
      title: self.$confs.shareTitle,
      path: url,
    }
  },

  computed: {
    res () {
      return this.$store.state.res;
    },
  },

  methods: {
    init () {
      let self = this;
      let userInfo = wx.getStorageSync('userInfo');
      self.userInfo = userInfo;

      let memberId = userInfo.id;
      // self.code = `${self.$confs.api}api/code/getCode?path=${self.$confs.shareUrl}&memberId=${memberId}`;
      self.code = `${self.$confs.api}api/code/getCode?path=${self.$confs.shareUrl}?memberId=${memberId}`;
    },
    save () {
      let self = this;
      // 获取图片
      wx.getImageInfo({
        src: self.code,
        success: function (res) {
          let path = res.path;

          self.$authorize({
            auth: 'scope.writePhotosAlbum',
            content: '微信保存图片需要您的授权，请前往设置'
          }).then((res) => {
            wx.saveImageToPhotosAlbum({
              filePath: path,
              success(result) {
                if (result.errMsg == 'saveImageToPhotosAlbum:ok') {
                  self.$toast.show({
                    title: '保存成功',
                    icon: 'success',
                    duration: 1500,
                  });
                }
              }
            });
          }).catch((res) => {
            self.$toast.show({
              title: '保存失败',
              icon: 'none',
              duration: 1500,
            });
          })
        }
      });
    },
  },

  mounted () {
    this.init();
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
